package com.haoshuang.zhouzhoubang.ui.page.message.child.order

import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.FragmentOrderMessageBinding
import com.haoshuang.zhouzhoubang.model.bean.MasterStoreOrderBean
import com.haoshuang.zhouzhoubang.ui.activity.ServiceOrderStatusActivity
import com.haoshuang.zhouzhoubang.ui.page.message.MessageFragment
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *  
 */
class OrderMessageFragment : BaseVMFragment<OrderMessageViewModel, FragmentOrderMessageBinding>() {

    companion object {
        fun getInstance(): OrderMessageFragment {
            val fragment = OrderMessageFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }
    private val data = arrayListOf<MasterStoreOrderBean>()
    private val adapter = OrderMessageAdapter(data)
    override fun initView(savedInstanceState: Bundle?) {

        binding.apply{
            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                vm.getStoreOrderMsg(p)
            }

            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                vm.getStoreOrderMsg(p)
            }

            recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
            adapter.setOnItemClick { view, position, item ->
                if (item == null) return@setOnItemClick
                vm.masterSetMessageRead(item.id, 3)
            }

            adapter.setOnItemChildClick(id = R.id.tvOrderDetail) { view, position, item ->
                item?.is_read = 1
                adapter.notifyItemChanged(position)
                vm.masterSetMessageRead(item?.id, 3)
                startPage<ServiceOrderStatusActivity>("ids" to item?.order_id)
            }
        }

    }

    override fun initData() {

    }

    override fun lazyLoadData() {

    }

    override fun onResume() {
        super.onResume()
        binding.refreshLayout.autoRefresh()
    }

    override fun startObserve() {
        vm.getStoreOrderMsgData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                val list = it?.data
                if (data.firstOrNull { it.isFirstRead == true } == null) {
                    val index = list?.indexOf(list.firstOrNull { it.is_read == 1 })
                    if (index != null && index != -1) {
                        list[index].isFirstRead = true
                    }
                }
                binding.refreshLayout.setupData(list, data, adapter, p)
                (parentFragment as MessageFragment).setUnReadButton(2, list?.firstOrNull { it.is_read == 0 } != null)
            }
        }

        vm.masterSetMessageReadData.observeState(this) {
            onSuccess { id ->
                val index = data.indexOf(data.firstOrNull { it.id == id })
                data[index].is_read = 1
                adapter.notifyItemChanged(index)
                (parentFragment as MessageFragment).refreshData()
            }
        }
    }
    fun setMessageRead() {
        data.forEach {
            it.is_read = 1
            it.isFirstRead = false
        }
        data.firstOrNull()?.isFirstRead = true
        adapter.notifyDataSetChanged()
    }
}